<template>
  <up-avatar :src="avatar" @click="onAvatar"></up-avatar>
  <up-action-sheet :actions="list" :show="show" round="24rpx" cancelText="取消" @close="onClose"
    @select="onSelect"></up-action-sheet>

</template>

<script setup>
  import {
    ref,
    reactive
  } from 'vue'
  const src = ref(
    'https://geiqiutiyu.oss-cn-hangzhou.aliyuncs.com/uploads/20240525/a4609b998176e96f6025ad2b4ea3e849.jpg')
  const list = ref([{
      name: '拍摄',
      type: 1
    },
    {
      name: '从手机相册',
      type: 2
    }
  ]);
  let avatar = ref("")
  let show = ref(false)

  function onAvatar() {
    show.value = true
  }

  function onSelect(e) {
    if (e.type === 1) {
      uploadImg('camera')
    } else if (e.type === 2) {
      uploadImg('album')
    }
  }

  function onClose() {
    show.value = false
  }
  //选择图片
  function uploadImg(sourceType) {
    const that_ = this
    //#ifdef APP-PLUS
    if (plus.navigator.checkPermission('android.permission.READ_EXTERNAL_STORAGE') !== 'authorized') {
      uni.showToast({
        title: '给球体育对存储空间/照片/相机权限申请说明：便于您使用该功能上传您的照片/图片/视频及用于更换头像、意见反馈、保存相册、发布等场景中读取和写入相册和文件内容。',
        icon: 'none',
        duration: 3000,
      });
    }
    plus.android.requestPermissions(['android.permission.READ_EXTERNAL_STORAGE'], function(e) {
      if (e.deniedAlways.length > 0) { //权限被永久拒绝
        // 弹出提示框解释为何需要定位权限，引导用户打开设置页面开启
        uni.showModal({
          content: '存储权限已被拒绝，请开启存储权限以便功能正常使用',
          confirmText: "前往设置",
          success: (res) => {
            if (res.confirm) {
              uni.openAppAuthorizeSetting({
                success(res) {
                  console.log(res)
                }
              })
            }
          }
        })
      }
      if (e.deniedPresent.length > 0) {
        uni.showModal({
          content: '存储权限已被拒绝，请开启存储权限以便功能正常使用',
          confirmText: "前往设置",
          success: (res) => {
            if (res.confirm) {
              uni.openAppAuthorizeSetting({
                success(res) {
                  console.log(res)
                }
              })
            }
          }
        })
      }
      if (e.granted.length > 0) { //权限被允许
        uni.chooseImage({
          count: 1,
          sizeType: ['original', 'compressed'],
          sourceType: [sourceType],
          success: function(res) {
            uploadFilePromise(res.tempFilePaths[0])
          }
        });
      }
    })
    //#endif
    uni.chooseImage({
      count: 1,
      sizeType: ['original', 'compressed'],
      sourceType: [sourceType],
      success: function(res) {
        uploadFilePromise(res.tempFilePaths[0])
      }
    });
  }
  //  上传头像
  function uploadFilePromise(url) {
    const token = uni.getStorageSync('userInfo').token
    uni.showToast({
      title: '上传中',
      icon: 'loading'
    })
    return new Promise((resolve, reject) => {
      let a = uni.uploadFile({
        url: `https://server.geiqiutiyu.com/api/common/upload`, //测试地址
        // url: 'https://localhost:8080/common/upload', //测试环境
        filePath: url,
        name: 'file',
        formData: {
          user: 'test'
        },
        header: {
          token: token
        },
        success: (res) => {
          const data = JSON.parse(res.data)
          if (data && data.code === 1) {
            resolve(data.data)
            avatar = data.data.fullurl
            this.$emit("UploadAvatar", avatar)
          } else {
            uni.showToast({
              title: data.msg,
              icon: "none"
            })
          }
        },
        fail(err) {
          uni.showToast({
            title: err.errMsg,
            icon: 'none'
          })
        }
      });
    })
  }
</script>

<style>
</style>